﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace MT_ToolKit
{
    public class TimerItem
    {
        /// <summary>
        /// 计时器ID
        /// </summary>
        private int Idx;
        /// <summary>
        /// 计时器显示的Text
        /// </summary>
        private Text _showText;
        /// <summary>
        /// 显示格式  时分秒-HHMMSS  分秒-MMSS
        /// </summary>
        private string _showFormat = "MMSS";
        /// <summary>
        /// 是否是倒计时
        /// </summary>
        private bool _bCountDown = false;
        /// <summary>
        /// 计时时长
        /// </summary>
        private int _currTime = 0;

        private bool isRun;

        private Action TimerEnd;

        public TimerItem(int idx, int duration = 0)
        {
            Idx = idx;
            _currTime = duration;
            if (duration > 0)
            {
                _bCountDown = true;
            }
        }
        
        public TimerItem SetShowText(Text txt, string format = "MMSS")
        {
            _showText = txt;
            _showFormat = format;
            FloatForTime(_currTime);
            return this;
        }

        public TimerItem OnStart()
        {
            isRun = true;
            return this;
        }

        public TimerItem OnPause()
        {
            isRun = false;
            return this;
        }

        public TimerItem SetTimerEndEvent(Action act)
        {
            TimerEnd = act;
            return this;
        }

        float _fTime;
        public void SetUpdate(float intervalTime)
        {
            if(!isRun) return;
            
            _fTime += intervalTime;
            if(_fTime > 1)
            {
                _fTime--;
                if (_bCountDown)
                {
                    _currTime--;
                    if (_currTime == 0)
                    {
                        TimerEnd?.Invoke();
                        isRun = false;
                    }
                        
                }
                else
                {
                    _currTime++;
                }
                FloatForTime(_currTime);
            }
        }

        private void FloatForTime(int time)
        {
            if (!_showText) return;
            //秒数取整
            int seconds = (int)time;
            if (_showFormat == "HHMMSS")
            {
                //一小时为3600秒 秒数对3600取整即为小时
                int hour = seconds / 3600;
                //一分钟为60秒 秒数对3600取余再对60取整即为分钟
                int minute = seconds % 3600 / 60;
                //对3600取余再对60取余即为秒数
                seconds = seconds % 3600 % 60;

                //打印00:00:00时间格式
                Debug.Log($"时间：{hour:D2}:{minute:D2}:{seconds:D2}");
                _showText.text = $"{hour:D2}:{minute:D2}:{seconds:D2}";
            }
            else if(_showFormat == "MMSS")
            {
                //一分钟为60秒 对60取整即为分钟
                int minute = seconds/ 60;
                //对60取余即为秒数
                seconds = seconds % 60;

                //打印00:00时间格式
                Debug.Log($"时间：{minute:D2}:{seconds:D2}");
                _showText.text = $"{minute:D2}:{seconds:D2}";
            }
        }
    }
}